package com.franklin.ideaplugin.mybatisrunner.xml.params;

import com.alibaba.fastjson2.annotation.JSONField;
import com.franklin.ideaplugin.api.utils.SwingUtils;
import com.franklin.ideaplugin.mybatisrunner.event.events.AbstractSqlEvent;
import com.franklin.ideaplugin.mybatisrunner.event.events.ParamUpdateEvent;
import com.intellij.psi.PsiElement;
import com.intellij.ui.DoubleClickListener;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.*;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.util.ui.FormBuilder;
import lombok.NoArgsConstructor;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author Ye Junhui
 * @since 2024/2/20
 */
public class NormalParam extends MybatisParam {

    private JBTextArea textArea = new JBTextArea();

    /**
     * 本文
     */
    private String text;

    public NormalParam(PsiElement psiElement) {
        super(psiElement);
    }

    @Override
    public JComponent buildComponent(Runnable action) {
        AtomicBoolean editing = new AtomicBoolean(false);
        textArea.addKeyListener(new KeyAdapter() {
            @Override
            public void keyReleased(KeyEvent e) {
                editing.set(true);
            }
        });
        textArea.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                if (editing.get()) {
                    action.run();
                    textArea.getParent().getParent().requestFocus();
                    editing.set(false);
                }
            }
        });
        textArea.setLineWrap(true);
//        textArea.addCaretListener();

        textArea.setBackground(JBColor.GRAY);
//        textArea.setPreferredSize(new Dimension(250,-1));
//        textArea.setMaximumSize(new Dimension(200,Integer.MAX_VALUE));
        JBScrollPane jbScrollPane = new JBScrollPane(textArea);
        jbScrollPane.setPreferredSize(new Dimension(200,60));
        jbScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        jbScrollPane.setMaximumSize(new Dimension(200,60));

        JBPanel<JBPanel> jbPanel = new JBPanel<>();
        jbPanel.setLayout(new GridLayoutManager(1,2));
        SwingUtils.addCell(
                jbPanel,
                new JBLabel(paramName),
                0,
                0,
                new Dimension(-1,-1)
        );
        SwingUtils.addCell(
                jbPanel,
                jbScrollPane,
                0,
                1,
                new Dimension(-1,-1)
        );


        return jbPanel;
    }

    @Override
    public AbstractSqlEvent buildEvent() {
        text = textArea.getText();
        return new ParamUpdateEvent(elExpression,paramName,text);
    }

    @Override
    public void load(String cache) {
        text = cache;
        textArea.setText(cache);
    }
}
